package com.xiaoke.liantongdemo.ui;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.xiaoke.liantongdemo.R;
import com.xiaoke.liantongdemo.bean.Group;
import com.xiaoke.liantongdemo.constants.Constants;
import com.xiaoke.liantongdemo.widget.BusinessesLayout;
import com.xiaoke.liantongdemo.widget.CategoryLayout;
import com.xiaoke.liantongdemo.widget.GroupLayout;
import com.xiaoke.liantongdemo.widget.MultiDirectionSlidingDrawer;
import com.xiaoke.liantongdemo.widget.MultiDirectionSlidingDrawer.OnDrawerCloseListener;
import com.xiaoke.liantongdemo.widget.MultiDirectionSlidingDrawer.OnDrawerOpenListener;
import com.xiaoke.liantongdemo.widget.MultiDirectionSlidingDrawer.OnDrawerScrollListener;
import com.xiaoke.liantongdemo.xml.XmlUtil;

public class LiantongDemoActivity extends Activity {
	/** Called when the activity is first created. */
	private final static int MAIN_INDEX = 0;
	private final static int G2_INDEX = 1;
	private final static int G3_INDEX = 2;
	private final static int NETWORK_INDEX = 3;
	private final static int MOBILE_CLIENT_INDEX = 4;
	private final static int SP_INDEX = 5;
	private final static int OFFICE_CLIENT_INDEX = 6;
	private MultiDirectionSlidingDrawer mdDrawer;
	private WebView content;
	private LinearLayout handle;
	private LinearLayout handle_view;

	private ImageView box_title;
	private RelativeLayout main_layout;
	private RelativeLayout g2_layout;
	private RelativeLayout g3_layout;
	private RelativeLayout network_layout;
	private RelativeLayout mobile_client_layout;
	private RelativeLayout sp_layout;
	private RelativeLayout office_client_layout;
	private RelativeLayout body;

	ArrayList<RelativeLayout> views = new ArrayList<RelativeLayout>();
	OnDrawerOpenListener drawerOpenListener = new OnDrawerOpenListener() {

		@Override
		public void onDrawerOpened() {
			handle.setBackgroundResource(R.drawable.handle_up);
			
		}
	};
	OnDrawerCloseListener closeListener = new OnDrawerCloseListener() {

		@Override
		public void onDrawerClosed() {
			handle.setBackgroundResource(R.drawable.handle_down);
			handle_view.setVisibility(View.VISIBLE);
			
//				handle.setText("手机报是一项资讯类业务，与媒体机构合作，通过彩信为您提供各类资讯信息的服务。手机报提供的资讯包括新闻、体育、娱乐、文化、生活、财经等，并以具体“报刊”产品体现相关内容。");
			
		}
	};

	OnDrawerScrollListener drawerScrollListener = new OnDrawerScrollListener() {

		@Override
		public void onScrollStarted() {
			if(!mdDrawer.isOpened()){
				handle_view.setVisibility(View.INVISIBLE);
//				handle.setText("");
			}else{
				
			}
			
		}

		@Override
		public void onScrollEnded() {
		
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(new BookShelfLayout(this, bookShelf));
		setContentView(R.layout.navigation_activity);
		init();
	}

	private void init() {
		mdDrawer = (MultiDirectionSlidingDrawer) findViewById(R.id.drawer);
		content = (WebView) mdDrawer.findViewById(R.id.web_view);
		handle = (LinearLayout) mdDrawer.findViewById(R.id.handle);
		handle_view=(LinearLayout) mdDrawer.findViewById(R.id.handle_view);
		mdDrawer.setOnDrawerOpenListener(drawerOpenListener);
		mdDrawer.setOnDrawerCloseListener(closeListener);
		mdDrawer.setOnDrawerScrollListener(drawerScrollListener);
		
		content.loadUrl("file:///android_asset/brand.html");
		box_title = (ImageView) findViewById(R.id.box_title);
		main_layout = (RelativeLayout) findViewById(R.id.main_page);
		g2_layout = (RelativeLayout) findViewById(R.id.g2_page);
		g3_layout = (RelativeLayout) findViewById(R.id.g3_page);
		network_layout = (RelativeLayout) findViewById(R.id.network_page);
		mobile_client_layout = (RelativeLayout) findViewById(R.id.mobile_client_page);
		sp_layout = (RelativeLayout) findViewById(R.id.sp_page);
		office_client_layout = (RelativeLayout) findViewById(R.id.office_client_page);
		views.add(main_layout);
		views.add(g2_layout);
		views.add(g3_layout);
		views.add(network_layout);
		views.add(mobile_client_layout);
		views.add(sp_layout);
		views.add(office_client_layout);
		body=(RelativeLayout) findViewById(R.id.main_body);
		body.setVisibility(View.INVISIBLE);

		main_layout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				changeStats(MAIN_INDEX);
				body.setVisibility(View.INVISIBLE);
			}
		});
		g2_layout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				changeStats(G2_INDEX);
				loadView(Constants.GROUP_2G_NAME);
				body.setVisibility(View.VISIBLE);

			}
		});
		g3_layout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				changeStats(G3_INDEX);
				loadView(Constants.GROUP_3G_NAME);
				body.setVisibility(View.VISIBLE);
			}
		});
		network_layout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				changeStats(NETWORK_INDEX);
				loadView(Constants.GROUP_INTERNET_NAME);
				body.setVisibility(View.VISIBLE);

			}
		});
		mobile_client_layout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				changeStats(MOBILE_CLIENT_INDEX);
				loadView(Constants.GROUP_PHONE_NAME);
				body.setVisibility(View.VISIBLE);

			}
		});
		sp_layout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				changeStats(SP_INDEX);
				loadView(Constants.GROUP_VALUE_NAME);
				body.setVisibility(View.VISIBLE);

			}
		});
		office_client_layout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				changeStats(OFFICE_CLIENT_INDEX);
				body.setVisibility(View.INVISIBLE);

			}
		});

	}

	private View loadView(String name) {
		int[] phone = { R.drawable.iphone4s, R.drawable.iphone4,
				R.drawable.blackberry9900, R.drawable.blackberry9800,
				R.drawable.blackberry9700, R.drawable.xiaomi, R.drawable.g18,
				R.drawable.i9100, R.drawable.g21, R.drawable.g11 };
		int index = 0;
		Group bookShelf = XmlUtil.getGroup(this, name);
		LinearLayout layout = (LinearLayout) findViewById(R.id.book_list);
		layout.removeAllViews();
		GroupLayout gLayout = new GroupLayout(this, bookShelf);
		if (name.equals(Constants.GROUP_PHONE_NAME)) {
			int size = ((LinearLayout) gLayout.getChildAt(0)).getChildCount();
			for (int i = 0; i < size; i++) {
				CategoryLayout categoryLayout = (CategoryLayout) ((LinearLayout) gLayout
						.getChildAt(0)).getChildAt(i);
				BusinessesLayout businessesLayout = (BusinessesLayout) categoryLayout
						.getChildAt(1);
				LinearLayout business_layout = (LinearLayout) businessesLayout
						.getChildAt(0);
				for (int j = 0; j < business_layout.getChildCount(); j++) {

					RelativeLayout busines_layout = (RelativeLayout) business_layout
							.getChildAt(j);
					ImageView view = (ImageView) busines_layout.getChildAt(0);
					view.setImageResource(phone[index]);
					index++;
				}

			}
		}
		layout.addView(gLayout);
		return layout;
	}

	private void changeStats(int item_id) {
		int[] background_no_press = { R.drawable.main1, R.drawable.g2_icon,
				R.drawable.g3_icon, R.drawable.network_icon,
				R.drawable.moblie_client, R.drawable.sp_icon01,
				R.drawable.office_client01 };
		int[] background_press = { R.drawable.main2, R.drawable.g2_icon2,
				R.drawable.g3_icon2, R.drawable.network_icon2,
				R.drawable.mobile_client2, R.drawable.sp_iocn2,
				R.drawable.office_client02 };
		int[] slider_tap = { R.drawable.slider_tap01, R.drawable.slider_tap02,
				R.drawable.slider_tap03, R.drawable.slider_tap04,
				R.drawable.slider_tap05, R.drawable.slider_tap06,
				R.drawable.slider_tap07 };
		for (int i = 0; i < views.size(); i++) {
			RelativeLayout layout = views.get(i);
			if (i == item_id) {
				layout.getChildAt(0).setVisibility(View.VISIBLE);
				((ImageView) layout.getChildAt(1))
						.setImageResource(background_press[i]);
				box_title.setBackgroundResource(slider_tap[i]);
			} else {
				layout.getChildAt(0).setVisibility(View.INVISIBLE);
				((ImageView) layout.getChildAt(1))
						.setImageResource(background_no_press[i]);
			}
		}
	}
}